package com.juno.cafenoa.cart;

import com.juno.cafenoa.Constants;
import com.juno.cafenoa.R;

import android.content.Context;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class OptionBasicItem extends LinearLayout {
	private String optionName;
	private int pricePerOne;
	private int optionAmount;

	public OptionBasicItem(Context context, final View parentView,
			String optionName, final int pricePerOne) {
		super(context);

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.layout_option, this);
		this.optionName = optionName;
		Log.d("OptionBasicItem.java", "parentView = " + parentView);
		// 샷수 변경시 가격 변경
		TextView txtvOptionName = (TextView) findViewById(R.id.txtvOptionAmount);
		txtvOptionName.setText(optionName);
		final EditText edtOptionAmount = (EditText) findViewById(R.id.edtOptionAmount);
		edtOptionAmount.addTextChangedListener(new TextWatcher() {
			TextView txtvPrice = (TextView) parentView
					.findViewById(R.id.txtvIndividualPrice);
			int beforeShot;

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				Log.d("addTextChangedListener",
						"addTextChangedListener is called");
				int afterShot = Integer.valueOf(s.toString());
				int nowPrice = Integer.valueOf(txtvPrice.getText() + "");
				Message msg;
				if (afterShot > beforeShot) {
					msg = Message.obtain(Cart.totalPricehandler,
							Constants.TOTAL_PRICE_ADDITION, pricePerOne);
					nowPrice += pricePerOne;
				} else {
					msg = Message.obtain(Cart.totalPricehandler,
							Constants.TOTAL_PRICE_SUBTRACTION, pricePerOne);
					nowPrice -= pricePerOne;
				}
				Cart.totalPricehandler.sendMessage(msg);
				txtvPrice.setText(nowPrice + "");

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				beforeShot = Integer.valueOf(s.toString());
			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});

		final int MAX_COFFEE_AMOUNT = 99;
		final int MIN_COFFEE_AMOUNT = 1;
		final int MAX_ADDITIONAL_AMOUNT = 99;
		final int MIN_ADDITIONAL_AMOUNT = 0;

		// 샷수 증/감 버튼
		Button btnShotAmountDown = (Button) findViewById(R.id.btnOptionAmountDown);
		btnShotAmountDown.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (optionAmount != MIN_ADDITIONAL_AMOUNT) {
					optionAmount--;
					edtOptionAmount.setText(optionAmount + "");
				}
			}
		});

		Button btnShotAmountUp = (Button) findViewById(R.id.btnOptionAmountUp);
		btnShotAmountUp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (optionAmount != MAX_ADDITIONAL_AMOUNT) {
					optionAmount++;
					edtOptionAmount.setText(optionAmount + "");
				}
			}
		});
	}

	public String getOptionName() {
		return optionName;
	}

	public void setOptionName(String optionName) {
		this.optionName = optionName;
	}

	public int getPricePerOne() {
		return pricePerOne;
	}

	public void setPricePerOne(int pricePerOne) {
		this.pricePerOne = pricePerOne;
	}

	public void setOptionAmount(int optionAmount) {
		this.optionAmount = optionAmount;
	}

	public int getOptionAmount() {
		return this.optionAmount;
	}

}
